using System.IO;
using NUnit.Framework;
using TDUnit.Common.TestDataParser;
using TDUnit.TestDataParsers.XmlParser;

namespace TDUnit.Tests
{
    [TestFixture]
    public class XmlTestDataParserTests : XmlTestDataParserBase
    {
        #region Parser setup tests
        [Test]
        public void ParserWillReturnDataSet()
        {
            const string dataset = @"<TestData><Table Name='TableName' PrimaryKey='PrimaryKey' IsAutoIdentity='True' /></TestData>";
            var parser = new XmlTestDataParser { TestDataString = dataset };
            Assert.IsNotNull(parser.Parse());
        }

        [Test]
        [ExpectedException(typeof(TestDataParserException))]
        public void ParserWithNoDataSetAndNoDataSetFilePathWillThrowExceptionOnParse()
        {
            new XmlTestDataParser().Parse();
        }

        [Test]
        public void ParserWillUseDataSetPathPropertyIfSet()
        {
            const string filePath = @"testFile.xml";
            const string dataset = @"<TestData><Table Name='TableName' PrimaryKey='PrimaryKey' IsAutoIdentity='True' /></TestData>";

            try
            {
                using (var s = File.CreateText(filePath))
                {
                    s.WriteLine(dataset);
                }
                var parser = new XmlTestDataParser { TestDataFilePath = filePath };
                Assert.IsNotNull(parser.Parse());
            }
            finally
            {
                File.Delete(filePath);
            }
        }

        [Test]
        [ExpectedException(typeof(TestDataParserException))]
        public void MissingRootElementCausesException()
        {
            const string dataset = @"<Table Name='TableName' PrimaryKey='PrimaryKey' IsAutoIdentity='True' />";
            var parser = new XmlTestDataParser { TestDataString = dataset };
            Assert.IsNotNull(parser.Parse());
        }
        #endregion
    }
}